package com.lanou.dllo.soccernews.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.dllo.soccernews.R;
import com.lanou.dllo.soccernews.bean.RankingsEntity;
import com.lanou.dllo.soccernews.volley.GetVolleyDatasUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/11/4.
 */
public class CentAdapter extends BaseAdapter {

    List<RankingsEntity> rankingsEntities;
    private LayoutInflater mInflater;
    private Context context;

    public CentAdapter(Context context) {
        mInflater = LayoutInflater.from(context);

        this.context = context;
    }
    public void addDatas(List<RankingsEntity> rankingsEntities) {
//        if (this.rankingsEntities == null) {
//            this.rankingsEntities = new ArrayList<>();
//        }
//        this.rankingsEntities.clear();
  //      this.rankingsEntities.addAll(rankingsEntities);
        this.rankingsEntities = rankingsEntities;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return rankingsEntities != null && rankingsEntities.size() > 0 ? rankingsEntities.size()
                : 0;
    }

    @Override
    public Object getItem(int position) {
        return rankingsEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CentViewHodle hodle;
        if (convertView == null) {
            hodle = new CentViewHodle();
            convertView = mInflater.inflate(R.layout.item_cent, parent,false);

            hodle.rank = (TextView) convertView.findViewById(R.id.cent_item_rankaa);
            hodle.club_name = (TextView) convertView.findViewById(R.id.cent_item_clubname);
            hodle.goals_pro = (TextView) convertView.findViewById(R.id.cent_item_goals_pro);
            hodle.matches_draw = (TextView) convertView.findViewById(R.id.cent_item_matches_draw);
            hodle.matches_lost = (TextView) convertView.findViewById(R.id.cent_item_matches_lose);
            hodle.matches_total = (TextView) convertView.findViewById(R.id.cent_item_matches_total);
            hodle.matches_won = (TextView) convertView.findViewById(R.id.cent_item_matches_won);
            hodle.points = (TextView) convertView.findViewById(R.id.cent_item_points);
            hodle.log = (ImageView) convertView.findViewById(R.id.cent_item_log);
            convertView.setTag(hodle);
        } else {
            hodle = (CentViewHodle) convertView.getTag();
        }
        RankingsEntity rankingsEntity = rankingsEntities.get(position);

        if (rankingsEntities != null && rankingsEntities.size()>0){
            Log.i("lanou",rankingsEntity.getRank()+"****");
            hodle.rank.setText(rankingsEntity.getRank());
            hodle.club_name.setText(rankingsEntity.getClub_name());
            hodle.matches_won.setText(rankingsEntity.getMatches_won());
            hodle.points.setText(rankingsEntity.getPoints());
            hodle.matches_total.setText(rankingsEntity.getMatches_total());
            hodle.matches_lost.setText(rankingsEntity.getMatches_lost());
            hodle.goals_pro.setText(rankingsEntity.getGoals_pro()+"/"+rankingsEntity.getGoals_against());
            hodle.matches_draw.setText(rankingsEntity.getMatches_draw());

            String teamId = rankingsEntity.getTeam_id();

            String logUrl = "http://img.dongqiudi.com/data/pic/"+teamId+".png";


            GetVolleyDatasUtil.getNetWorkImg(logUrl,hodle.log);

        }

        return convertView;
    }

    class CentViewHodle {
        TextView rank, club_name, matches_total, matches_won, matches_draw,
                matches_lost, goals_pro, points;
        ImageView log;
    }
}
